import os

import matplotlib.pyplot as plt
import numpy as np
from adjustText import adjust_text
from dateutil.utils import today
from numpy import sort
from openpyxl import Workbook, load_workbook
# y轴百分比、小数处理包
from matplotlib import ticker

# 显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']

wb = Workbook()
wb = load_workbook('直分图表数据py.xlsx', data_only=True)
sheet = wb['Sheet1']

DATE = [str(sheet['BE3'].value)[5:10], str(sheet['BA3'].value)[5:10], str(sheet['AW3'].value)[5:10],
        str(sheet['AS3'].value)[5:10], str(sheet['AO3'].value)[5:10], str(sheet['AK3'].value)[5:10],
        str(sheet['AG3'].value)[5:10], str(sheet['AC3'].value)[5:10], str(sheet['Y3'].value)[5:10],
        str(sheet['U3'].value)[5:10], str(sheet['Q3'].value)[5:10], str(sheet['M3'].value)[5:10],
        str(sheet['I3'].value)[5:10], str(sheet['E3'].value)[5:10]]

ZF_RATE_HB = [[], [], [], [], [], [], [], []]
ZF_RATE_XB = [[], [], [], [], [], []]
ZF_RATE_DB = [[], [], [], [], []]
ZF_RATE_HZ = [[], [], [], [], [], []]
for i in range(0, 8):
    ZF_RATE_HB[i] = [sheet['BG' + str(i + 65)].value, sheet['BC' + str(i + 65)].value, sheet['AY' + str(i + 65)].value,
                     sheet['AU' + str(i + 65)].value,
                     sheet['AQ' + str(i + 65)].value, sheet['AM' + str(i + 65)].value, sheet['AI' + str(i + 65)].value,
                     sheet['AE' + str(i + 65)].value, sheet['AA' + str(i + 65)].value,
                     sheet['W' + str(i + 65)].value, sheet['S' + str(i + 65)].value, sheet['O' + str(i + 65)].value,
                     sheet['K' + str(i + 65)].value, sheet['G' + str(i + 65)].value]
for i in range(0, 6):
    ZF_RATE_XB[i] = [sheet['BG' + str(i + 87)].value, sheet['BC' + str(i + 87)].value, sheet['AY' + str(i + 87)].value,
                     sheet['AU' + str(i + 87)].value,
                     sheet['AQ' + str(i + 87)].value, sheet['AM' + str(i + 87)].value, sheet['AI' + str(i + 87)].value,
                     sheet['AE' + str(i + 87)].value, sheet['AA' + str(i + 87)].value,
                     sheet['W' + str(i + 87)].value, sheet['S' + str(i + 87)].value, sheet['O' + str(i + 87)].value,
                     sheet['K' + str(i + 87)].value, sheet['G' + str(i + 87)].value]
for i in range(0, 5):
    ZF_RATE_DB[i] = [sheet['BG' + str(i + 81)].value, sheet['BC' + str(i + 81)].value, sheet['AY' + str(i + 81)].value,
                     sheet['AU' + str(i + 81)].value,
                     sheet['AQ' + str(i + 81)].value, sheet['AM' + str(i + 81)].value, sheet['AI' + str(i + 81)].value,
                     sheet['AE' + str(i + 81)].value, sheet['AA' + str(i + 81)].value,
                     sheet['W' + str(i + 81)].value, sheet['S' + str(i + 81)].value, sheet['O' + str(i + 81)].value,
                     sheet['K' + str(i + 81)].value, sheet['G' + str(i + 81)].value]
for i in range(0, 6):
    ZF_RATE_HZ[i] = [sheet['BG' + str(i + 74)].value, sheet['BC' + str(i + 74)].value, sheet['AY' + str(i + 74)].value,
                     sheet['AU' + str(i + 74)].value,
                     sheet['AQ' + str(i + 74)].value, sheet['AM' + str(i + 74)].value, sheet['AI' + str(i + 74)].value,
                     sheet['AE' + str(i + 74)].value, sheet['AA' + str(i + 74)].value,
                     sheet['W' + str(i + 74)].value, sheet['S' + str(i + 74)].value, sheet['O' + str(i + 74)].value,
                     sheet['K' + str(i + 74)].value, sheet['G' + str(i + 74)].value]

x1 = np.array(DATE)  # x_日期
y_hb = []
for i in range(0, 8):
    y_hb.append(np.array(ZF_RATE_HB[i]))

y_xb = []
for i in range(0, 6):
    y_xb.append(np.array(ZF_RATE_XB[i]))

y_db = []
for i in range(0, 5):
    y_db.append(np.array(ZF_RATE_DB[i]))

y_hz = []
for i in range(0, 6):
    y_hz.append(np.array(ZF_RATE_HZ[i]))

fig = plt.figure(figsize=(16, 9))
plt.suptitle(str(sheet['E3'].value)[5:10][0:2]+'月'+str(sheet['E3'].value)[5:10][3:5]+'日'+'分拨直分占比曲线（三）', fontsize=18)
plt.axis('off')
# 子图1华北
ax1 = fig.add_subplot(221)
plt.plot(x1, y_hb[0], 'blue', label='北京')
plt.plot(x1, y_hb[1], 'red', label='唐山')
plt.plot(x1, y_hb[2], 'yellow', label='天津')
plt.plot(x1, y_hb[3], 'green', label='石市')
plt.plot(x1, y_hb[4], 'purple', label='保定')
plt.plot(x1, y_hb[5], 'orange', label='邯郸')
plt.plot(x1, y_hb[6], 'teal', label='衡水')
plt.plot(x1, y_hb[7], 'brown', label='呼市')
ax1.legend(loc='upper center', bbox_transform=ax1.transAxes, ncol=4, frameon=False)  # 设置图例
lim_y_hb = [sort(y_hb[0])[13], sort(y_hb[1])[13], sort(y_hb[2])[13], sort(y_hb[3])[13], sort(y_hb[4])[13],
            sort(y_hb[5])[13],
            sort(y_hb[6])[13], sort(y_hb[7])[13]]
lim_y_hb.sort()
plt.ylim(0, lim_y_hb[7] + 0.15)  # y轴取值范围
# plt.xlabel('日期')
plt.ylabel('直分占比')
plt.title('华北（8）', fontsize=14)
ax1.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
texts = [plt.text(x1[13], y_hb[i][13], '%.2f%%' % (y_hb[i][13] * 100), fontsize=10, verticalalignment='bottom',
                  horizontalalignment='center') for i in range(0, 8)]
# adjust_text(texts,
#             arrowprops=dict(
#                 arrowstyle='-',  # 箭头样式
#                 lw=0.5,  # 线宽
#                 color='black')  # 箭头颜色
#             )

# 子图2西北
ax1 = fig.add_subplot(222)
plt.plot(x1, y_xb[0], 'blue', label='西安')
plt.plot(x1, y_xb[1], 'red', label='太原')
plt.plot(x1, y_xb[2], 'green', label='兰州')
plt.plot(x1, y_xb[3], 'yellow', label='银川')
plt.plot(x1, y_xb[4], 'purple', label='西宁')
plt.plot(x1, y_xb[5], 'brown', label='乌市')
ax1.legend(loc='upper center', bbox_transform=ax1.transAxes, ncol=3, frameon=False)  # 设置图例
lim_y_xb = [sort(y_xb[0])[13], sort(y_xb[1])[13], sort(y_xb[2])[13], sort(y_xb[3])[13], sort(y_xb[4])[13],
            sort(y_xb[5])[13]]
lim_y_xb.sort()
plt.ylim(0, lim_y_xb[5] + 0.15)  # y轴取值范围
# plt.xlabel('日期')
plt.ylabel('直分占比')
plt.title('西北（6）', fontsize=14)
ax1.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
texts = [plt.text(x1[13], y_xb[i][13], '%.2f%%' % (y_xb[i][13] * 100), fontsize=10, verticalalignment='bottom',
                  horizontalalignment='center') for i in range(0, 6)]
# adjust_text(texts,
#             arrowprops=dict(
#                 arrowstyle='-',  # 箭头样式
#                 lw=0.5,  # 线宽
#                 color='black')  # 箭头颜色
#             )


# 子图3东北
ax1 = fig.add_subplot(223)
plt.plot(x1, y_db[0], 'blue', label='哈市')
plt.plot(x1, y_db[1], 'red', label='长春')
plt.plot(x1, y_db[2], 'green', label='沈阳')
plt.plot(x1, y_db[3], 'brown', label='盘锦')
plt.plot(x1, y_db[4], 'purple', label='大连')
ax1.legend(loc='upper center', bbox_transform=ax1.transAxes, ncol=3, frameon=False)  # 设置图例
lim_y_db = [sort(y_db[0])[13], sort(y_db[1])[13], sort(y_db[2])[13], sort(y_db[3])[13], sort(y_db[4])[13]]
lim_y_db.sort()
plt.ylim(0, lim_y_db[4] + 0.15)  # y轴取值范围
plt.xlabel('日期')
plt.ylabel('直分占比')
plt.title('东北（5）', fontsize=14)
ax1.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
texts = [plt.text(x1[13], y_db[i][13], '%.2f%%' % (y_db[i][13] * 100), fontsize=10, verticalalignment='bottom',
                  horizontalalignment='center') for i in range(0, 5)]
# adjust_text(texts,
#             arrowprops=dict(
#                 arrowstyle='-',  # 箭头样式
#                 lw=0.5,  # 线宽
#                 color='black')  # 箭头颜色
#             )
# 子图4华中
ax1 = fig.add_subplot(224)
plt.plot(x1, y_hz[0], 'blue', label='武汉')
plt.plot(x1, y_hz[1], 'red', label='荆州')
plt.plot(x1, y_hz[2], 'green', label='郑州')
plt.plot(x1, y_hz[3], 'purple', label='商丘')
plt.plot(x1, y_hz[4], 'teal', label='新乡')
plt.plot(x1, y_hz[5], 'brown', label='漯河')
ax1.legend(loc='upper center', bbox_transform=ax1.transAxes, ncol=3, frameon=False)  # 设置图例
lim_y_hz = [sort(y_hz[0])[13], sort(y_hz[1])[13], sort(y_hz[2])[13], sort(y_hz[3])[13], sort(y_hz[4])[13],
            sort(y_hz[5])[13]]
lim_y_hz.sort()
plt.ylim(0, lim_y_hz[5] + 0.15)  # y轴取值范围
plt.xlabel('日期')
plt.ylabel('直分占比')
plt.title('华中（6）', fontsize=14)
texts = [plt.text(x1[13], y_hz[i][13], '%.2f%%' % (y_hz[i][13] * 100), fontsize=10, verticalalignment='bottom',
                  horizontalalignment='center') for i in range(0, 6)]
# adjust_text(texts,
#             arrowprops=dict(
#                 arrowstyle='-',  # 箭头样式
#                 lw=0.5,  # 线宽
#                 color='black')  # 箭头颜色
#             )
plt.show()
# plt.savefig(fname='D:\\Desktop\\直分图表\\直分率\\2023'+str(today())[5:7] + str(today())[8:10]+'\\05-华北西北华中东北'+str(today())[5:7]+str(today())[8:10]+'.png')
# print('05华北西北华中东北'+str(today())[5:7]+str(today())[8:10]+'.png'+'已保存')
# print('制图完成')
